#include "home_widget.h"
#include "ui_home_widget.h"
#include "QDebug"
#include "QSqlError"
#include "QMouseEvent"
#include "QPainter"
#include "QFileDialog"
#include "QBuffer"
#include "QMessageBox"

#include "teacherwidget.h"

home_widget::home_widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::home_widget)
{
    ui->setupUi(this);
    //连接信号与槽
    connect(ui->pushButton,&QPushButton::clicked,this,&home_widget::on_pushButton_clicked);



    ui->photo->setPixmap(QPixmap(":/image/plus.png").scaled(ui->photo->width(),ui->photo->height()));
    photo=*ui->photo->pixmap();

//    db=QSqlDatabase::addDatabase("QMYSQL");//添加mysql
//    db.setHostName("192.168.10.128");
//    db.setUserName("root");
//    db.setPassword("123456");
//    db.setDatabaseName("test268");
//    db.setPort(3306);
//    if(!db.open()){
//        qDebug()<<db.lastError()<<endl;
//        return;
//    }
//    QSqlQuery query(db);//语句执行对象
//    if(query.exec("select *from teacher;")==false){
//        qDebug()<<db.lastError()<<endl;
//        return;
//    }

    sql.ConnectSql("192.168.175.131");
    sql.SetFakeStuInfo();


    pushFlag=0;
    connect(ui->pB_photo,&QPushButton::clicked,[this](){
        if(pushFlag==0){
                QString path=QFileDialog::getOpenFileName(this,"获取一张图片",":/");
                if(!path.isEmpty())
                {
                    QPixmap pixmap(path);
                    QPixmap chopped=pixmap.scaled(ui->photo->width(),ui->photo->height(),Qt::KeepAspectRatioByExpanding);
                    QRect chopRect((chopped.width()-ui->photo->width())/2,0,ui->photo->width(),ui->photo->height());
                    ui->photo->setPixmap(chopped.copy(chopRect));
                    photo=*ui->photo->pixmap();
                    pushFlag=1;
                    ui->pB_photo->setText("确认修改");
                }
        }
        else if (pushFlag==1) {
            QByteArray  imageInfo;
            QBuffer buffer(&imageInfo);//QT缓冲区对象
            buffer.open(QIODevice::WriteOnly);//以只写打开
            photo.toImage().save(&buffer,"PNG");//以PNG格式存入缓冲区   写法适用多媒体文件数据

            QSqlQuery query(sql.Getdb());
            //sql语句是字符串：二进制数据是不能直接放入字符串的
            query.prepare("insert into image(image) values(:imageData);");//冒号变量不直接作为字符串交付的指令，会在后续进行绑定
            query.bindValue(":imageData",imageInfo);//把二进制数据绑定到变量上
            if(query.exec()){
                QMessageBox::information(this,"提示","照片修改成功");
                pushFlag=0;
                ui->pB_photo->setText("修改照片");
            }
            else{
                qDebug()<<sql.Getdb().lastError()<<endl;
            }
        }

    });


     QSqlQuery query(sql.Getdb());
    if(query.exec("select image from image where id=2;")){
        if(query.next()){
            QByteArray imageInfo= query.value("image").toByteArray();//取出图片
            QPixmap m;
            m.loadFromData(imageInfo);
            ui->photo->setPixmap(m);
            photo=*ui->photo->pixmap();
        }
    }

    if(query.exec("select * from student_list where id=1;")){
        if(query.next()){
            ui->name_2->setText(query.value("Name").toString());
            ui->name_2->setReadOnly(true);
            ui->part_2->setText(query.value("Sex").toString());
            ui->part_2->setReadOnly(true);
            ui->email_2->setText(query.value("teacherId").toString());
            ui->email_2->setReadOnly(true);
        }
    }

//    if(query.exec("select * from course where teacher='张老师';")){
//        while(query.next()){
//            ui->course_2->addItem(query.value("name").toString());
//        }
//    }
    connect(ui->course_2,SIGNAL(currentIndexChanged(const QString&)),this,SLOT(onComboBoxChanged(const QString&)));



}

void home_widget::SetTeacher(const QString &TeaID)
{
    this->TeaID=TeaID;
}



home_widget::~home_widget()
{
    delete ui;
}



void home_widget::mouseMoveEvent(QMouseEvent *e)//在ui设置mouseTracking
{
    QPoint labelPos =ui->photo->mapFromParent(e->pos());
    //鼠标指向标签界面所在矩形框内
    if(ui->photo->rect().contains(labelPos)){
        ui->photo->setPixmap(QPixmap(":/image/plus.png").scaled(ui->photo->size()));
    }
    else{
        ui->photo->setPixmap(photo);
    }
}

void home_widget::onComboBoxChanged(const QString &text)
{
    QSqlQuery query;
    QString sql=QString("select * from course where teacher='张老师' ; ");
    if(query.exec(sql)){

        qDebug()<<text<<endl;

        while(query.next()){
            ui->class_3->addItem(query.value("class").toString());
        }
    }
    else{
        //qDebug()<<db.lastError()<<endl;

    }
}



void home_widget::on_pushButton_clicked()
{
    //创建并显示另一个界面
    teacherwidget *teacher_views=new teacherwidget;
    teacher_views->show();
}
